package com.osmino.contacts.list;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Filter;
import android.widget.Filterable;
import com.osmino.contacts.entity.Contact;
import com.osmino.contacts.entity.Contact.PhotoState;
import com.osmino.contacts.provider.AsyncPhotoLoader;

public class ContactsBaseAdapter extends BaseAdapter implements Filterable {

	protected final Context mContext;
	protected List<Contact> mListOriginalContacts;
	protected final List<Object> mListItems;
	protected Filter mFilter;
	protected String mConstraint;
	protected int mFrom = -1;
	protected boolean mShowCheckboxes;
	protected boolean mShowAlphabet;
	public static final int FROM_CONTACTS = 1;
	public static final int FROM_GROUPS = 2;
	public static final int FROM_MERGE = 3;
	
	public ContactsBaseAdapter(Context context, int from) {
		mContext = context;
		mFrom = from;
		mListItems = new ArrayList<Object>();
		mConstraint = null;
	}

	@Override
	public int getCount() {
		return mListItems.size();
	}

	@Override
	public Object getItem(int position) {
		return mListItems.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		View item = null;
		if (convertView == null) {
			item = newView(position);
		} else {
			item = convertView;
		}
		bindView(position, item);
		return item;
	}
	
	protected View newView(int position) {
		ContactItem item = new ContactItem(mContext);
		return item;
	}
	
	protected void bindView(int position, View view) {
		ContactItem contactItem = (ContactItem) view;
		contactItem.showToggle(mShowCheckboxes);
		Contact contact = (Contact) mListItems.get(position);
		if (contact.getPhotoState() == PhotoState.NOT_LOADED) {
			new AsyncPhotoLoader(mContext, this).execute(contact);
		}
		contactItem.setInfo(contact, mConstraint);
		contactItem.setAlphabetIndexerOffset(mShowAlphabet);
		if(mFrom == FROM_MERGE){
			contactItem.setAlphabetIndexerOffset(true);
		}
	}

	@Override
	public Filter getFilter() {
		if (mFilter == null) {
			mFilter = new DisplayNameFilter();
		}
		return mFilter;
	}

	protected void refreshListContactInfo(List<Contact> listContact) {
		mListItems.clear();
		mListItems.addAll(listContact);
		notifyDataSetChanged();
	}

	public void setListContacts(List<Contact> listContacts) {
		Collections.sort(listContacts, new Contact.ContactComparator());
		mListOriginalContacts = new ArrayList<Contact>(listContacts);
		refreshListContactInfo(listContacts);
	}
	
	public void setShowCheckboxes(boolean showCheckboxes) {
		mShowCheckboxes = showCheckboxes;
	}
	
	public void setIsAbcIndexVisible(boolean isVisible){
		mShowAlphabet = isVisible;
	}
	
	private class DisplayNameFilter extends Filter {

		public DisplayNameFilter() {//
		}

		@Override
		protected FilterResults performFiltering(CharSequence prefix) {
			FilterResults results = new FilterResults();
			List<Contact> resultList = new ArrayList<Contact>();
			for (Contact contact : mListOriginalContacts) {
				String displayName = contact.displayName;
				if (displayName.contains(prefix)) {
					resultList.add(contact);
				}
			}
			results.values = resultList;
			results.count = resultList.size();
			return results;
		}

		@SuppressWarnings("unchecked")
		@Override
		protected void publishResults(CharSequence constraint, FilterResults results) {
			mConstraint = constraint.toString();
			refreshListContactInfo((List<Contact>) results.values);
		}
	}
}
